﻿using StackExchange.Redis;
namespace WebApplication1.Cache
{
    //用GPT生成
    public class RedisHelper
    {
        private readonly ConnectionMultiplexer _redis;
        private readonly IDatabase _database;

        public RedisHelper(string connectionString, int database = 0)
        {
            _redis = ConnectionMultiplexer.Connect(connectionString);
            _database = _redis.GetDatabase(database);
        }

        // Set a value in Redis
        public bool SetValue(string key, string value)
        {
            return _database.StringSet(key, value);
        }

        // Get a value from Redis
        public string GetValue(string key)
        {
            return _database.StringGet(key);
        }

        // Delete a value from Redis
        public bool DeleteValue(string key)
        {
            return _database.KeyDelete(key);
        }

        // Check if a key exists in Redis
        public bool KeyExists(string key)
        {
            return _database.KeyExists(key);
        }

        // Close the Redis connection
        public void Close()
        {
            _redis.Close();
        }
    }
}
